package com.example.madman.yuejian.Fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.example.madman.yuejian.Adapter.FragmentAdapter;
import com.example.madman.yuejian.R;

import java.util.ArrayList;
import java.util.List;


public class TabFragment extends Fragment implements  ViewPager.OnPageChangeListener,View.OnClickListener{

    private ViewPager viewPager;
    private List<android.support.v4.app.Fragment> listfragment;
    private Button clubbutton,coursebutton,coachbutton;
    public TabFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tab, container, false);

        viewPager = view.findViewById(R.id.viewpage);
        clubbutton = view.findViewById(R.id.button_club);
        coursebutton = view.findViewById(R.id.button_course);
        coachbutton = view.findViewById(R.id.button_coach);
        initview();
        return view;
    }

    private void initview(){

        clubbutton.setOnClickListener(this);
        coursebutton.setOnClickListener(this);
        coachbutton.setOnClickListener(this);

        listfragment = new ArrayList<>();
        listfragment.add(new ClubListFragment());
        listfragment.add(new CourseListFragment());
        listfragment.add(new CoachListFragment());
        viewPager.setAdapter(new FragmentAdapter(getFragmentManager(),listfragment));
        viewPager.addOnPageChangeListener(this);
        viewPager.setCurrentItem(0);


    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        initBtnListener();
        switch (position){
            case 0:
                clubbutton.setBackgroundColor(Color.parseColor("#ff735c"));
                break;
            case 1:
                coursebutton.setBackgroundColor(Color.parseColor("#ff735c"));
                break;
            case 2:
                coachbutton.setBackgroundColor(Color.parseColor("#ff735c"));
                break;

        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {



    }

    @Override
    public void onClick(View view) {
        initBtnListener();
        switch (view.getId()){
            case R.id.button_club:
                clubbutton.setBackgroundColor(Color.parseColor("#ff735c"));
                viewPager.setCurrentItem(0);
                break;
            case R.id.button_course:
                coursebutton.setBackgroundColor(Color.parseColor("#ff735c"));
                viewPager.setCurrentItem(1);
                break;
            case R.id.button_coach:
                coachbutton.setBackgroundColor(Color.parseColor("#ff735c"));
                viewPager.setCurrentItem(2);
                break;

        }

    }
    private void initBtnListener(){
        clubbutton.setBackgroundResource(R.color.main_title_text_normal);
        coachbutton.setBackgroundResource(R.color.main_title_text_normal);
        coursebutton.setBackgroundResource(R.color.main_title_text_normal);
    }
}
